package com.ktg.mes.rec.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktg.common.utils.SecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktg.mes.rec.mapper.RecPowderedRecordMapper;
import com.ktg.mes.rec.domain.RecPowderedRecord;
import com.ktg.mes.rec.service.IRecPowderedRecordService;



/**
 * 加粉记录Service业务层处理
 *
 * @author byteco
 * @date 2022-10-31
 */
@Service
public class RecPowderedRecordServiceImpl extends ServiceImpl<RecPowderedRecordMapper, RecPowderedRecord> implements IRecPowderedRecordService
{
    @Autowired
    private RecPowderedRecordMapper recPowderedRecordMapper;



    /**
     * 查询加粉记录
     *
     * @param entity 加粉记录
     * @param page 分页对象
     * @return 加粉记录
     */
    @Override
    public IPage<RecPowderedRecord> queryPageList(RecPowderedRecord entity, Page<RecPowderedRecord> page) {
        LambdaQueryWrapper<RecPowderedRecord> lqw = buildQueryWrapper(entity);
        lqw.orderByDesc(RecPowderedRecord::getCreateTime);
        IPage<RecPowderedRecord> pageList = this.page(page, lqw);
        return pageList;
    }

    /**
     * 查询加粉记录
     *
     * @param entity 加粉记录
     * @return 加粉记录
     */
    @Override
    public List<RecPowderedRecord> queryList(RecPowderedRecord entity) {
        LambdaQueryWrapper<RecPowderedRecord> lqw = buildQueryWrapper(entity);
        lqw.orderByDesc(RecPowderedRecord::getCreateTime);
        return this.list(lqw);
    }

    public RecPowderedRecord getInfo(Long id)
    {
        QueryWrapper<RecPowderedRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(RecPowderedRecord::getPowderedId, id);
        return this.getOne(queryWrapper);
    }


    /**
     * 新增加粉记录
     *
     * @param entity 加粉记录
     * @return 结果
     */
    @Override
    public void insert(RecPowderedRecord entity)
    {
        entity.setCreateTime(new Date());
        entity.setCreateBy(SecurityUtils.getUsername());
        this.save(entity);
    }

    /**
     * 修改加粉记录
     *
     * @param entity 加粉记录
     * @return 结果
     */
    @Override
    public boolean update(RecPowderedRecord entity)
    {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(new Date());
        return this.updateById(entity);
    }

    /**
     * 批量删除加粉记录
     *
     * @param powderedIds 需要删除的加粉记录主键
     * @return 结果
     */
    @Override
    public boolean delete(Long[] powderedIds)
    {

        return this.removeByIds(Arrays.asList(powderedIds));
    }

    /**
     * 删除加粉记录信息
     *
     * @param powderedId 加粉记录主键
     * @return 结果
     */
    @Override
    public boolean delete(Long powderedId)
    {
        return this.removeById(powderedId);
    }

    private LambdaQueryWrapper<RecPowderedRecord> buildQueryWrapper(RecPowderedRecord entity) {
        LambdaQueryWrapper<RecPowderedRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(entity.getWorkshopId() != null, RecPowderedRecord::getWorkshopId, entity.getWorkshopId());
        lqw.eq(entity.getMachineId() != null, RecPowderedRecord::getMachineId, entity.getMachineId());
        lqw.like(StringUtils.isNotBlank(entity.getProcessName()), RecPowderedRecord::getProcessName, entity.getProcessName());
        lqw.like(StringUtils.isNotBlank(entity.getToolTypeCode()), RecPowderedRecord::getToolTypeCode, entity.getToolTypeCode());
        lqw.like(StringUtils.isNotBlank(entity.getToolTypeName()), RecPowderedRecord::getToolTypeName, entity.getToolTypeName());
        lqw.eq(StringUtils.isNotBlank(entity.getSpecification()), RecPowderedRecord::getSpecification, entity.getSpecification());
        lqw.like(StringUtils.isNotBlank(entity.getTaskName()), RecPowderedRecord::getTaskName, entity.getTaskName());
        lqw.eq(entity.getStartTime() != null, RecPowderedRecord::getStartTime, entity.getStartTime());
        lqw.eq(StringUtils.isNotBlank(entity.getDuration()), RecPowderedRecord::getDuration, entity.getDuration());
        return lqw;
    }
}
